/**
 * 自选课程简介及目录
 */
var localStorageManagers = require("script/localStorageManagers.js");
//mui初始化
mui.init({
	swipeBack: true
});
//初始化plus
mui.plusReady(plusReady);
//plusReady
function plusReady() {
	//懒加载
	mui('#viewtip').imageLazyload({
		placeholder: '../../images/coursedefault.png'
	});
	
	var self = plus.webview.currentWebview();
	document.getElementById('fCourseid').value = self.courseid;
	if(self.type==2)document.getElementById('nav-mui-bar').style.display='block';
	//加载课程信息
	loadCourseinfo();
	//点击目录事件
	mui('.catalog-div').on('tap', 'label', showResView);
	//添加课程
	mui('#nav-mui-bar').on('tap','#addcourse',addcourse);
	//课题树伸缩
	mui('.catalog-div').on('tap','pre',function(){
		if($(this).hasClass('tom')){
			 $(this).removeClass("tom");
                $(this).addClass("rit");
                $(this).parent("a").parent("li").children("ul").stop(true, true).slideUp(300);
                $(this).parent("a").parent("li").children("p").slideUp(300);
		}else{
			 $(this).removeClass("rit");
                $(this).addClass("tom");
                $(this).parent("a").parent("li").children("ul").stop(true, true).slideDown(300);
                $(this).parent("a").parent("li").children("p").slideDown(300);
		}
	})
}
//加载课程信息
function loadCourseinfo() {
	//ajax加载课程信息
	loadCourseinfoAjax();
}
//ajax加载课程信息
function loadCourseinfoAjax() {
	var courseid = document.getElementById('fCourseid').value;
	//ajax获取课程信息
	var url = serverAjax + 'mobile/course/getSchoolCourseinfo.do';
	mui.ajax(url, {
		data: {
			courseid: courseid
		},
		dataType: 'json',
		type: 'post',
		timeout: 20000,
		success: function(data) {
			//课程简介，教师
			var title=data.course.title;
			var courseteacher = data.course.creator;
			var intro = data.course.description;
			document.getElementById('coursetilte').innerHTML=title;
			document.getElementById('courseteacher').innerText = courseteacher;
			document.getElementById('courseintor').innerText = intro;
			//课程图片
			var imgpath='../../images/coursedefault.png';
			if(data.course.imagePath.length>0)imgpath=fileServerAjax+data.course.imagePath;
			document.getElementById('courseimg').setAttribute('src', imgpath);
			//目录
			if(data.catalogs.childCatalogs[0]!=null){
			var catalogs = data.catalogs.childCatalogs[0].childCatalogs;
			var catalogsstr = '';
			var ulname="leftmenu";
			catalogsstr = buildcatalogdiv(catalogs, catalogsstr,ulname);
			document.getElementById('catalog-div').innerHTML = catalogsstr;
			}
		},
		error: function(xhr, type, errorThrown) {
			ajaxErrorTip();
			console.log(xhr);
		}
	});
}
//构建目录
function buildcatalogdiv(catalogs, catalogsstr,ulname) {
	catalogsstr += '<ul class="'+ulname+'">';
	for (var i = 0; i < catalogs.length; i++) {
		var catalog = catalogs[i];
		var childsize = 0;
		if (catalog.childCatalogs != null) childsize = catalog.childCatalogs.length;
		var catalogname=catalog.name;
		if (catalogname != null && catalogname.length > 10) catalogname = catalogname.substring(0, 10) + '...';
		catalogsstr += '<li><a href="#" catalogid="'+catalog.catalogId+'"><label catalogid="'+catalog.catalogId+'">' + catalogname + '</label>';
		if (childsize > 0)catalogsstr += '<pre class="rit"></pre>';
		catalogsstr += '</a>';
		if (childsize > 0) catalogsstr = buildcatalogdiv(catalog.childCatalogs, catalogsstr,ulname+"-c");
		catalogsstr += '</li>';
	}
	catalogsstr += '</ul>';
	return catalogsstr;
}
//点击课题
function showResView(e) {
	var courseid = document.getElementById('fCourseid').value;
	var catalogid = this.getAttribute('catalogid');
	//courseid=109;
	if (courseid <= 0 || catalogid <= 0) return;
	mui.openWindow({
		url: '../../selfcourseview/resView.html',
		id: 'selfcourseview/resView',
		extras: {
			'courseid': courseid,
			'catalogid': catalogid
		}
	});
	e.stopPropagation();
}
//添加课程
function addcourse(){
	var thisbtn=this;
	var courseid = document.getElementById('fCourseid').value;
	var userid = localStorageManagers.getUseridSession();
	var url=serverAjax+"mobile/course/addSelfCourse.do"
	mui.ajax(url,{
		data:{'userid':userid,'courseids':courseid},
		dataType:'json',
		type:'post',
		timeout:10000,
		success:function(data){
			mui.toast('添加成功');
			var parentWebView = plus.webview.getWebviewById("selfcenter/selfstudycourse/addselfcourse");
	        parentWebView.evalJS("loadCourse('"+courseid+"')");
	        var parentWebView1 = plus.webview.getWebviewById("selfcenter/selfstudycourse/studycourseindex");
	        parentWebView1.evalJS("loadCourse()");
	        document.getElementById('nav-mui-bar').style.display='none';
	 		//mui.back();
		},
		error: function(xhr, type, errorThrown) {
			mui.toast('该课程已添加');
			 //ajaxErrorTip();
			//异常处理；
			console.log(xhr);
		}
	});
}
